package design.pattern.观察者模式;

import java.util.ArrayList;
import java.util.List;

/**
 * 目标/通知者
 */
public abstract class Subject<T> {

    private List<Obeserver> list = new ArrayList<>();

    private T status;

    public void attach(Obeserver obeserver){
        list.add(obeserver);
    }
    public void detach(Obeserver obeserver){
        list.remove(obeserver);
    }

    private void notifyObeserver(){
        for(Obeserver obeserver : list)
            obeserver.update(status);
    }

    public void change(T status){
        this.status = status;
        notifyObeserver();
    }

}
